package 算法;

import java.util.LinkedList;
import java.util.Scanner;


public class WireRouter {
    public int[][] grid;
    public int size_h;
    public int size_l;
    public int pathLen;
    public LinkedList<Position> q;
    public Position start;
    public Position finish;
    public Position[] path;
    public WireRouter(int[][] grid,int size_h,int size_l,Position start,Position finish){
        this.grid=grid;
        this.size_h=size_h;
        this.size_l=size_l;
        this.start=start;
        this.finish=finish;
    }

    //方格所在位置
    public static class Position{
        public int row;//行
        public int col;//列

        public Position(int r ,int c){
            row=r;
            col=c;
        }
    }


    public boolean findPath(){
        if(start.row==finish.row&&start.col==finish.col){
            pathLen=0;
            return true;
        }

        Position[] offset=new Position[4];
        offset[0]=new Position(0,1);
        offset[1]=new Position(1,0);
        offset[2]=new Position(0,-1);
        offset[3]=new Position(-1,0);//表明移动方向


        for(int i=0;i<=size_h+1;i++){
            grid[0][i]=grid[size_h+1][i]=1;//设定上方和下方

        }

        for(int i=0;i<=size_l+1;i++){
            grid[i][0]=grid[i][size_l+1]=1;//左边和右边
        }

        Position here=new Position(start.row,start.col);
        grid[start.row][start.col]=2;
        int numOfNbrs=4;//相邻方格数


        q=new LinkedList<Position>();
        Position nbr=new Position(0,0);
        do{

            for(int i=0;i<numOfNbrs;i++){
                nbr.row=here.row+offset[i].row;
                nbr.col=here.col+offset[i].col;
                if(grid[nbr.row][nbr.col]==0){
                    grid[nbr.row][nbr.col]=grid[here.row][here.col]+1;
                    if(nbr.row==finish.row&&nbr.col==finish.col)
                        break;
                    q.add(new Position(nbr.row,nbr.col));
                }
            }

            //检测目标位置
            if(nbr.row==finish.row&&nbr.col==finish.col)
                break;
            if(q.isEmpty())
                return false;

            here=q.poll();
        }while(true);

        //最短路径
        pathLen=grid[finish.row][finish.col]-2;
        path=new Position[pathLen];

        //回溯
        here=finish;
        for(int j=pathLen-1;j>=0;j--){
            path[j]=here;

            for(int i=0;i<numOfNbrs;i++){
                nbr.row=here.row+offset[i].row;
                nbr.col=here.col+offset[i].col;
                if(grid[nbr.row][nbr.col]==j+2)
                    break;
            }
            here=new Position(nbr.row,nbr.col);
        }

        System.out.println(pathLen);


        System.out.print(start.row+","+start.col+"-->");
        for(int j=0;j<pathLen-1;j++){
            System.out.print(path[j].row+","+path[j].col+"-->");
        }
        System.out.println(finish.row+","+finish.col);

        return true;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        String s1=sc.nextLine();
        String[] s2=s1.split(" ");
        Integer size_h=Integer.parseInt(s2[0]);
        Integer size_l=Integer.parseInt(s2[1]);

        int[][] grid=new int[size_h+2][size_l+2];

        for(int i=1;i<=size_h;i++){
            String str=sc.nextLine();
            String[] strs=str.split(" ");
            for(int j=0;j<strs.length;j++){
                grid[i][j+1]=Integer.parseInt(strs[j]);
            }
        }


        String s3=sc.nextLine();
        String[] s4=s3.split(" ");
        int startRow=Integer.parseInt(s4[0]);
        int startCol=Integer.parseInt(s4[1]);
        Position start=new Position(startRow,startCol);

        
        String s5=sc.nextLine();
        String[] s6=s5.split(" ");
        int finishRow=Integer.parseInt(s6[0]);
        int finishCol=Integer.parseInt(s6[1]);
        Position finish=new Position(finishRow,finishCol);

        WireRouter w=new WireRouter(grid,size_h,size_l,start,finish);

        w.findPath();

    }
}
